//
//  LoginVC.swift
//  Xiaozihui
//
//  Created by 校企荟 on 2017/10/9.
//  Copyright © 2017年 校企荟. All rights reserved.
//

import UIKit

class LoginVC: BaseVC {

    lazy var usernameTF: UITextField = {
        let tf = UITextField()
        tf.placeholder = "手机号"
        tf.keyboardType = .numberPad
        tf.borderStyle = .none
        tf.clearButtonMode = .always
        return tf
    }()
    
    lazy var passwordTF: UITextField = {
        let tf = UITextField()
        tf.placeholder = "密码"
        tf.isSecureTextEntry = true
        tf.borderStyle = .none
        tf.clearButtonMode = .always
        return tf
    }()
    
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        
        btn.setAttributedTitle(NSAttributedString(string: "登  录", attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 20),NSForegroundColorAttributeName : UIColor.white]), for: .normal)
        btn.layer.cornerRadius = 3
        btn.layer.masksToBounds = true;
        btn.backgroundColor = mainColor
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white

    }

    override func setupUI() {
        
        view.addSubview(usernameTF)
        view.addSubview(passwordTF)
        view.addSubview(loginBtn)
        
        usernameTF.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(230 * ScaleX)
            make.centerX.equalTo(view)
            make.width.equalTo(280 * ScaleX)
        }
        
        passwordTF.snp.makeConstraints { (make) in
            make.top.equalTo(usernameTF.snp.bottom).offset(25 * ScaleX)
            make.centerX.equalTo(view)
            make.width.equalTo(usernameTF)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(passwordTF).offset(70 * ScaleX)
            make.centerX.equalTo(view)
            make.width.equalTo(usernameTF)
            make.height.equalTo(40 * ScaleX)
        }
        
        
        loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        let usernameLine = UIView()
        usernameLine.backgroundColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
        usernameTF.addSubview(usernameLine)
        usernameLine.snp.makeConstraints { (make) in
            make.width.equalTo(usernameTF)
            make.bottom.equalTo(usernameTF)
            make.centerX.equalTo(usernameTF)
            make.height.equalTo(1)
        }
        
        let passwordLine = UIView()
        passwordLine.backgroundColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
        passwordTF.addSubview(passwordLine)
        passwordLine.snp.makeConstraints { (make) in
            make.width.equalTo(passwordTF)
            make.bottom.equalTo(passwordTF)
            make.centerX.equalTo(passwordTF)
            make.height.equalTo(1)
        }
    }
    
    //登录
    @objc private  func login() -> () {
        ProgressHUD.show()
        
        //TODO: 删除下一行代码
        
//        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kLoginSuccessNotificationName), object: nil)
//        ProgressHUD.dismiss()
        
        AccountManager.sharedManager.login(username: usernameTF.text! , password: passwordTF.text!) { (isSuccess) in
            
            if isSuccess {
                ProgressHUD.showSuccess(status: "登录成功")
           
            } else {
                ProgressHUD.showError(status: "登录失败,请重试")
            }
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for view in view.subviews {
            view.resignFirstResponder()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
